package cn.yangliu.jsw;

import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;

/**
 * The type Jsw helper.
 *
 * <p>
 * .
 *
 * @author 问道于盲
 */
public class JswHelper implements CommandLineRunner, WrapperListener {

	/**
	 * current class's static member
	 * The constant LOGGER.
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(JswHelper.class);

	/**
	 * Run.
	 *
	 * @param args the args
	 */
	@Override
	public void run(String... args) {
		WrapperManager.start(this, args);
	}

	/**
	 * Start integer.
	 *
	 * @param strings the strings
	 * @return the integer
	 */
	@Override
	public Integer start(String[] strings) {
		return null;
	}

	/**
	 * Stop int.
	 *
	 * @param i the
	 * @return the int
	 */
	@Override
	public int stop(int i) {
		WrapperManager.hasShutdownHookBeenTriggered();
		return i;
	}

	/**
	 * Control event.
	 *
	 * @param event the event
	 */
	@Override
	public void controlEvent(int event) {
		if (event == 202) {
			if (!(WrapperManager.isLaunchedAsService())) {
				WrapperManager.stop(0);
			}
		} else if (event != 200) {
			WrapperManager.stop(0);
		}
	}

	/**
	 * Destroy.
	 */
	@PreDestroy
	public void destroy(){
		LOGGER.info("destory method has been invoked,jsw application will be shutdown soon!");
	}
}
